<template>
  <view class="image-upload-container">
    <view class="upload-title">{{ title }}</view>
    <view class="image-list">
      <!-- 已上传图片列表 -->
      <view class="image-item" v-for="(item, index) in imageList" :key="index">
        <image :src="item" mode="aspectFill" class="uploaded-image"></image>
        <view class="delete-btn" @click="removeImage(index)">
          <text class="delete-icon">×</text>
        </view>
      </view>
      
      <!-- 上传按钮 -->
      <view class="upload-btn" @click="chooseImage" v-if="imageList.length < maxCount">
        <text class="upload-icon">+</text>
        <text class="upload-text">上传图片</text>
      </view>
    </view>
    <view class="upload-tip">{{ imageList.length }}/{{ maxCount }}</view>
  </view>
</template>

<script>
export default {
  name: 'ImageUpload',
  props: {
    title: {
      type: String,
      default: '图片上传'
    },
    maxCount: {
      type: Number,
      default: 9
    },
    uploadUrl: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      imageList: []
    }
  },
  methods: {
    // 选择图片
    chooseImage() {
      const that = this;
      uni.chooseImage({
        count: this.maxCount - this.imageList.length,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          that.uploadImages(res.tempFilePaths);
        },
        fail(err) {
          console.error('选择图片失败:', err);
          uni.showToast({
            title: '选择图片失败',
            icon: 'none'
          });
        }
      });
    },
    
    // 上传图片
    uploadImages(tempFilePaths) {
      const that = this;
      tempFilePaths.forEach((tempFilePath, index) => {
        uni.uploadFile({
          url: this.uploadUrl,
          filePath: tempFilePath,
          name: 'file',
          success(res) {
            if (res.statusCode === 200) {
              try {
                const data = JSON.parse(res.data);
                if (data.code === 0 && data.data && data.data.url) {
                  that.imageList.push(data.data.url);
                  that.$emit('change', that.imageList);
                } else {
                  uni.showToast({
                    title: data.message || '上传失败',
                    icon: 'none'
                  });
                }
              } catch (e) {
                console.error('解析上传结果失败:', e);
                uni.showToast({
                  title: '解析上传结果失败',
                  icon: 'none'
                });
              }
            } else {
              uni.showToast({
                title: `上传失败，状态码: ${res.statusCode}`,
                icon: 'none'
              });
            }
          },
          fail(err) {
            console.error('上传图片失败:', err);
            uni.showToast({
              title: '上传图片失败',
              icon: 'none'
            });
          }
        });
      });
    },
    
    // 删除图片
    removeImage(index) {
      this.imageList.splice(index, 1);
      this.$emit('change', this.imageList);
    }
  }
}
</script>

<style lang="scss" scoped>
.image-upload-container {
  padding: 20rpx;
  
  .upload-title {
    font-size: 32rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
  }
  
  .image-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;
    
    .image-item {
      position: relative;
      width: 160rpx;
      height: 160rpx;
      border-radius: 12rpx;
      overflow: hidden;
      
      .uploaded-image {
        width: 100%;
        height: 100%;
      }
      
      .delete-btn {
        position: absolute;
        top: 0;
        right: 0;
        width: 40rpx;
        height: 40rpx;
        background-color: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        
        .delete-icon {
          color: #fff;
          font-size: 36rpx;
        }
      }
    }
    
    .upload-btn {
      width: 160rpx;
      height: 160rpx;
      border: 2rpx dashed #ddd;
      border-radius: 12rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: #999;
      
      .upload-icon {
        font-size: 60rpx;
      }
      
      .upload-text {
        font-size: 24rpx;
        margin-top: 10rpx;
      }
    }
  }
  
  .upload-tip {
    font-size: 24rpx;
    color: #999;
    margin-top: 20rpx;
    text-align: right;
  }
}
</style>    